<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 定义数组arr
    let arr = [50,80,5,7,0,6,13]
    //封装函数;形参array接受数组，flag用来接受true或false
    function handleData(array, flag = true){
      let sum = 0 //定义和的容器
      // 将flag转换为Boolean类型
      flag = Boolean(flag)
      for(let i = 0;i<=array.length-1;i++){
        sum = array[i]+sum
      }
      // 如果flag为true则返回和,为false则返回数组平均值
      if(flag === true){
        return `数组和为${sum}`
      }else if(flag === false) {
        return `数组平均值为${sum / array.length}`
      }
    }
    // 调用函数handleData
    let res = handleData(arr,undefined) // true和不穿为求和，false为平均值
    document.write(res)
  </script>
</body>
</html>